package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IMemberService;
import com.mall.user.IRegisterService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.KaptchaCodeRequest;
import com.mall.user.dto.KaptchaCodeResponse;
import com.mall.user.dto.UserRegisterRequest;
import com.mall.user.dto.UserRegisterResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("user")
public class RegisterController {
    @Reference
    private IKaptchaService iKaptchaService;
    @Reference(check = false,retries = 0)
    private IRegisterService registerService;

    @Reference(check = false, retries = 0)
    private IMemberService memberService;

    @Reference(check = false ,retries = 0)
    IKaptchaService kaptchaService;
    @PostMapping("register")
    @Anoymous
    public ResponseData register(@RequestBody Map<String,String> map, HttpServletRequest servletRequest){
        String username = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");
        String email = map.get("email");

        //判断验证码是否正确
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        kaptchaCodeRequest.setCode(captcha);
        String kaptcha_uuid = CookieUtil.getCookieValue(servletRequest, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        Boolean kaptchaCodeIsRight = validateKaptchaCode(kaptchaCodeRequest, servletRequest);

        //注册：向用户表中插入记录
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setEmail(email);
        userRegisterRequest.setUserName(username);
        userRegisterRequest.setUserPwd(userPwd);
        UserRegisterResponse registerResponse = registerService.register(userRegisterRequest);
        if(registerResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return  new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil<>().setErrorMsg(registerResponse.getMsg());
    }

    /**
     * 判断验证码是否正确
     *
     * @param servletRequest
     * @return
     */
    private Boolean validateKaptchaCode(KaptchaCodeRequest kaptchaCodeRequest, HttpServletRequest servletRequest) {

        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
       /* if (kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS)) {
            return new ResponseUtil().setData(null);
        }
        return new ResponseUtil().setErrorMsg(Integer.parseInt(SysRetCodeConstants.KAPTCHA_CODE_ERROR.getCode()),
                SysRetCodeConstants.KAPTCHA_CODE_ERROR.getMessage());*/
        if (kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return true;
        }
        return false;
    }

    @GetMapping("verify")
    @Anoymous
    public ResponseData verifyUser(String uid, String username){
        if (uid == null || username == null) {
            return new ResponseUtil().setErrorMsg(SysRetCodeConstants.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        Integer result = memberService.verifyUser(uid, username);
        return result == 1 ? new ResponseUtil().setData(null) :
                new ResponseUtil().setErrorMsg(SysRetCodeConstants.USER_INFOR_INVALID.getMessage());
    }
}
